﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace HQWeb.SysManage.ThemeWeddingManage
{
    public partial class ThemeWedding : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            VerdictIsLogin();
            if (!IsPostBack)
            {
                BindDropDownList();
                string strType = Request["type"].Trim();
                Session["ThemeFile"] = new List<Model.Progect.T_PUB_FILE>();
                Session["ThemeProject"] = new List<Model.SysManage.THEME_PROJECT>();
                Session["ThemeProjectFile"] = new List<Model.Progect.T_PUB_PROJECTFILE>();
                if (strType.Equals("edit"))     //编辑进入
                {
                    //labTitle.Text = "编辑作业配置";

                    btn_Add.CommandName = "edit";
                    hidID.Value = Request["id"];  //编辑ID
                    FillPage(hidID.Value);
                }
                else if (strType.Equals("add"))   //添加进入
                {

                }
            }
        }

        //编辑时页面填充
        private void FillPage(string strID)
        {
            Model.SysManage.T_PUB_WEDDINGTHEME model = new Model.SysManage.T_PUB_WEDDINGTHEME();
            BLL.SysManage.T_PUB_WEDDINGTHEME bll = new BLL.SysManage.T_PUB_WEDDINGTHEME();
            model = bll.GetModel(strID);
            txtName.Text = model.TITLE;
            txtDescription.Text = model.DESCRIPTION;
            drop_color.SelectedValue = model.WEDDING_COLOR;
            wending_month.SelectedValue = model.WEDDING_MONTH;
            string strDropPrice = model.WEDDING_PRICE.ToString();
            if (drop_price.Items.FindByValue(strDropPrice) != null)
            {
                drop_price.SelectedValue = strDropPrice;
            }
            string strType = model.Type;
            if (dropType.Items.FindByValue(strType) != null)
            {
                dropType.SelectedValue = strType;
            }

            wedding_hotel.Text = model.HOTEL_NAME;
            this.txtFileUrl.Text = model.FILE_URL;
            BLL.Progect.T_PUB_FILE pfbll = new BLL.Progect.T_PUB_FILE();
            List<Model.Progect.T_PUB_FILE> fileList = pfbll.GetFileList(strID);
            Session["ThemeFile"] = fileList;
            //BLL.Progect.T_PUB_PROJECT pjbll = new BLL.Progect.T_PUB_PROJECT();
            //List<Model.Progect.T_PUB_PROJECT> projectList = pjbll.GetProjecList(strID);
            BLL.SysManage.THEME_PROJECT pjbll = new BLL.SysManage.THEME_PROJECT();
            List<Model.SysManage.THEME_PROJECT> projectList = pjbll.GetProjecList(strID);
            Session["ThemeProjectFile"] = new BLL.Progect.T_PUB_PROJECTFILE().GetModelList(string.Format(" FILE_ID='{0}'", strID));
            Session["ThemeProject"] = projectList;
            BindFile();
            BindProject();
            string strJS = "bindstyle('" + model.WEDDING_STYLE + "')";
            Universal.JS.RunJS(strJS);
        }

        protected void BindFile()
        {
            List<Model.Progect.T_PUB_FILE> modelList = (List<Model.Progect.T_PUB_FILE>)Session["ThemeFile"];
            List<Model.Progect.T_PUB_PROJECTFILE> plist = (List<Model.Progect.T_PUB_PROJECTFILE>)Session["ThemeProjectFile"];
            var objs = from a in modelList join b in plist on a.FILE_ID equals b.PROJECT_ID orderby b.ORDER_INDEX 
                        select new { a.FILE_ID,a.FILE_NAME,a.FILE_TITEL,a.ISFLAG,a.TYPE };
            repeater1.DataSource = objs.ToList();
            repeater1.DataBind();
        }

        protected void BindProject() 
        {
            List<Model.SysManage.THEME_PROJECT> modelList = (List<Model.SysManage.THEME_PROJECT>)Session["ThemeProject"];
            repeater2.DataSource = modelList;
            repeater2.DataBind();
        }

        protected void btnGetProject_Click(object sender, EventArgs e)
        {
            BindProject();
        }

        protected void btnGetFile_Click(object sender, EventArgs e)
        {
            BindFile();
        }

        protected void btnDELFile_Click(object sender, EventArgs e)
        {
            string strID = delFileID.Value;
            List<Model.Progect.T_PUB_FILE> modelList = (List<Model.Progect.T_PUB_FILE>)Session["ThemeFile"];
            Model.Progect.T_PUB_FILE model = modelList.Find(p => p.FILE_ID == strID);
            modelList.Remove(model);
            BindFile();
            string strCachName = "Exhibition_" + Session["BRANCH_ID"].ToString();
            Universal.Caches.RemoveIdentify(strCachName);
        }
        protected void btnUP_Click(object sender, EventArgs e)
        {
            string strID = hidUpID.Value;
            //List<Model.Progect.T_PUB_FILE> modelList = (List<Model.Progect.T_PUB_FILE>)Session["ThemeFile"];
            //Model.Progect.T_PUB_FILE model = modelList.Find(p => p.FILE_ID == strID);
            //int maxtIndex=modelList.Max(p=>p.
            //BindFile();
            string strCachName = "Exhibition_" + Session["BRANCH_ID"].ToString();
            Universal.Caches.RemoveIdentify(strCachName);
        }

        protected void btnDELProject_Click(object sender, EventArgs e)
        {
            string strID = delProjectID.Value;
            List<Model.SysManage.THEME_PROJECT> modelList = (List<Model.SysManage.THEME_PROJECT>)Session["ThemeProject"];
            Model.SysManage.THEME_PROJECT model = modelList.Find(p => p.ProjectID == strID);
            modelList.Remove(model);
            BindProject();
            string strCachName = "Exhibition_" + Session["BRANCH_ID"].ToString();
            Universal.Caches.RemoveIdentify(strCachName);
        }

        #region 保存事情

        protected void btn_Add_Click(object sender, EventArgs e)
        {
            string strComandName = btn_Add.CommandName.Trim();
            BLL.SysManage.T_PUB_WEDDINGTHEME bll = new BLL.SysManage.T_PUB_WEDDINGTHEME();
            BLL.Progect.T_PUB_FILE filebll = new BLL.Progect.T_PUB_FILE();
            Model.SysManage.T_PUB_WEDDINGTHEME model;
            string strID = hidID.Value.Trim();
            if (strComandName.Equals("edit"))
            {
                model = bll.GetModel(strID);
            }
            else
            {
                model = new Model.SysManage.T_PUB_WEDDINGTHEME();
                model.ID = System.Guid.NewGuid().ToString() + "-" + Session["BRANCH_ID"].ToString();
            }
            model.TITLE = txtName.Text.Trim();
            model.WEDDING_COLOR = drop_color.SelectedValue;
            model.WEDDING_MONTH = wending_month.SelectedValue;
            if (drop_price.SelectedValue!="")
            {
                model.WEDDING_PRICE = int.Parse(drop_price.SelectedValue);
            }

            model.Type = dropType.SelectedValue;

            if (txtFileUrl.Text.Trim() == "")
            {
                model.FILE_URL = txtName.Text.Trim();
            }
            else
            {
                model.FILE_URL = txtFileUrl.Text.Trim();
            }
            model.DESCRIPTION = txtDescription.Text.Trim();
            model.HOTEL_NAME = wedding_hotel.Text.Trim();
            model.WEDDING_STYLE = hidWeddingStyle.Value;
            model.FLAG = true;
            model.BRANCH_ID = int.Parse(Session["BRANCH_ID"].ToString());
            List<Model.Progect.T_PUB_FILE> modelFileList = (List<Model.Progect.T_PUB_FILE>)Session["ThemeFile"];
            IList<Model.Progect.T_PUB_FILE> addList = new List<Model.Progect.T_PUB_FILE>();
            IList<Model.Progect.T_PUB_FILE> editList = new List<Model.Progect.T_PUB_FILE>();
            IList<Model.Order.T_PUB_Order_Projectcs> ThemeProject = new List<Model.Order.T_PUB_Order_Projectcs>();
            List<Model.SysManage.THEME_PROJECT> modelProjectList = (List<Model.SysManage.THEME_PROJECT>)Session["ThemeProject"];

            foreach (Model.SysManage.THEME_PROJECT item in modelProjectList)
            {
                Model.Order.T_PUB_Order_Projectcs modelProject = new Model.Order.T_PUB_Order_Projectcs();
                modelProject.Number = item.Number;
                modelProject.OriderID = model.ID;
                modelProject.ProjectID = item.ProjectID;
                modelProject.Remark = "主题婚礼项目";
                ThemeProject.Add(modelProject);
            }

            if (strComandName.Equals("edit"))
            {
                foreach (Model.Progect.T_PUB_FILE item in modelFileList)
                {
                    if (item.ISFLAG == false)
                    {
                        //item.ISFLAG = true;
                        //item.FILE_FORM = "6,";
                        addList.Add(item);
                    }
                    else
                    {
                        //if (item.FILE_FORM.IndexOf("6") == -1)
                        //{
                        //    item.FILE_FORM += "6,";
                        //}
                        editList.Add(item);
                    }
                }
                bll.Update(model, addList, editList, ThemeProject);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "更新" + model.TITLE + "主题婚礼信息", DateTime.Now);
                //MessageBox.Show(this, "修改成功");
                filebll.SynchronizaionThemeToAllBranch(model.ID,Convert.ToInt32(model.BRANCH_ID));
            }
            else
            {
                foreach (Model.Progect.T_PUB_FILE item in modelFileList)
                {
                    if (item.ISFLAG == false)
                    {
                        //item.ISFLAG = true;
                        //item.FILE_FORM = "6,";
                        addList.Add(item);
                    }
                    else
                    {
                        //if (item.FILE_FORM.IndexOf("6") == -1)
                        //{
                        //    item.FILE_FORM += "6,";
                        //}
                        editList.Add(item);
                    }
                }
                bll.Add(model, addList, editList, ThemeProject);
                Universal.CommonMethod.SaveOpreation(Convert.ToInt32(Session["USER_ID"]), "添加" + model.TITLE + "主题婚礼信息", DateTime.Now);
                filebll.SynchronizaionThemeToAllBranch(model.ID, Convert.ToInt32(model.BRANCH_ID));
            }
            Session.Remove("ThemeFile");
            Session.Remove("ThemeProject");
            Response.Redirect("ThemeWeddingList.aspx");
        }

        #endregion

        #region 初始化页面下拉框
        public void BindDropDownList()
        {
            BLL.SysManage.T_PUB_DICTIONARY bll = new BLL.SysManage.T_PUB_DICTIONARY();
            List<Model.SysManage.T_PUB_DICTIONARY> il = bll.GetListArray(" FLAG = 1 order by ORDER_ID ");
            BindDropData(drop_color, 4, il);
            BindDropData(wending_month, 7, il);
            BindDropData(drop_price, 6, il);
            BindDropData(dropType, 2, il);
            IEnumerable<Model.SysManage.T_PUB_DICTIONARY> lo = il.Where(p => p.PARENT_ID == 5);
            string x = "w";
            BindStyle(x, lo, addStyle);
        }
        #endregion

        #region 初始化风格
        protected void BindStyle(string y, IEnumerable<Model.SysManage.T_PUB_DICTIONARY> il, System.Web.UI.HtmlControls.HtmlTableCell controls)
        {
            StringBuilder strHtml = new StringBuilder();
            strHtml.Append("  <table width=\"90%\" ");
            int count = il.Count();
            int tempValue;
            for (int j = 0; j <= (count - 1) / 5; j++)
            {
                int i = 0;
                strHtml.Append(" <tr>");
                for (; i < 5 && 5 * j + i < count; i++)
                {
                    tempValue = 5 * j + i;
                    string strName = il.ElementAt(tempValue).NAME;
                    string strId = il.ElementAt(tempValue).ID.ToString();
                    //int temp = 5 * j + i;
                    strHtml.Append("<td align=\"left\" style=\"width:20%\">");
                    strHtml.Append(" <input type=\"checkbox\" name=\"" + y + "style\" id=\"" + y + strId + "\" value=\"" + strId + "\"/>");
                    strHtml.Append(strName);
                    strHtml.Append("</td>");
                }
                if ((5 - i) > 0)
                {
                    int sy = 5 - i;
                    strHtml.Append("<td colspan=" + sy + ">");
                    strHtml.Append("</td>");
                }
            }
            strHtml.Append(" </table>");
            controls.InnerHtml = strHtml.ToString();
        }
        #endregion

        #region 通过属性类型获取属性并绑定到下拉框
        protected void BindDropData(DropDownList drop_name, int type, List<Model.SysManage.T_PUB_DICTIONARY> il)
        {

            IEnumerable<Model.SysManage.T_PUB_DICTIONARY> lo = il.Where(p => p.PARENT_ID == type);
            drop_name.ClearSelection();
            drop_name.DataSource = lo;
            drop_name.DataTextField = "NAME"; //dropdownlist的Text的字段 
            drop_name.DataValueField = "ID";//dropdownlist的Value的字段 
            drop_name.DataBind();
            ListItem item = new ListItem("", "");
            item.Selected = true;
            drop_name.Items.Add(item);
        }
        #endregion

        protected void btnaddNum_Click(object sender, EventArgs e)
        {
            string strID = NumProjectID.Value;
            List<Model.SysManage.THEME_PROJECT> modelList = (List<Model.SysManage.THEME_PROJECT>)Session["ThemeProject"];
            Model.SysManage.THEME_PROJECT model = modelList.Find(p => p.ProjectID == strID);
            model.Number += 1;
            BindProject();
        }

        protected void btnsubtractNum_Click(object sender, EventArgs e)
        {
            string strID = NumProjectID.Value;
            List<Model.SysManage.THEME_PROJECT> modelList = (List<Model.SysManage.THEME_PROJECT>)Session["ThemeProject"];
            Model.SysManage.THEME_PROJECT model = modelList.Find(p => p.ProjectID == strID);
            if (model.Number > 1)
            {
                model.Number -= 1;
                BindProject();
            }
        }
    }
}